package com.codeying.servlet;

import com.codeying.entity.LoginUser;
import com.codeying.utils.JdbcUtils;
import com.codeying.utils.MyUtils;
import com.codeying.utils.component.ApiResult;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 欢迎页
 */
@WebServlet("/hello")
public class HelloServlet extends AbsServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        LoginUser loginUser = MyUtils.getUser(req);
        //
        req.getRequestDispatcher("/hello.jsp").forward(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        resp.setCharacterEncoding("UTF-8");
        //数据
        List<Echart> l = new ArrayList<>();
        //图表1..
        Echart echart = new Echart();
        echart.setName("XXX数据");
        echart.setDtos(list("select name,value from ..."));

        //l.add(echart.init()); //TODO　记得打开注释！
        resp.getWriter().print(ApiResult.successData(l));
    }

    public List<Dto> list(String sql){
        try{
            QueryRunner qr = new QueryRunner();
            System.out.println("查询sql："+sql);
            //执行查询
            return qr.query(JdbcUtils.getConnection(), sql, new BeanListHandler<>(Dto.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public static class Echart {
        String name;
        List<Dto> dtos = new ArrayList<>();
        String type;

        List<String> names = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        public Echart init(){
            for (Dto dto : dtos) {
                names.add(dto.getName());
                values.add(dto.getValue());
            }
            return this;
        }

        public List<String> getNames() {
            return names;
        }
        public List<Double> getValues() {
            return values;
        }
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Dto> getDtos() {
            return dtos;
        }

        public void setDtos(List<Dto> dtos) {
            this.dtos = dtos;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }

    public static class Dto {
        String name;
        Double value;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Double getValue() {
            return value;
        }

        public void setValue(Double value) {
            this.value = value;
        }
    }
}
